/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sessionBeans;

import entity.Claim;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Minh Luc
 */
@Stateless
public class ClaimFacade extends AbstractFacade<Claim> implements ClaimFacadeLocal {

    @PersistenceContext(unitName = "WebBasedClaimProcessing-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ClaimFacade() {
        super(Claim.class);
    }

    public List<Claim> findByUserIdSend(Integer UserId) {
        String sQuery = "SELECT c FROM Claim c WHERE c.users.userId = :userId ORDER BY c.dateSend DESC";
        Query query = em.createQuery(sQuery);
        query.setParameter("userId", UserId);
        return query.getResultList();
    }

    public BigDecimal getClaimMoneyByUserId(Integer UserId) {
        String sQuery = "SELECT c FROM Claim c WHERE c.users.userId = :userId";// cho nay nho them status
        Query query = em.createQuery(sQuery);
        query.setParameter("userId", UserId);
        BigDecimal decimal = new BigDecimal(0);
        List<Claim> list = query.getResultList();
        if (list.size() > 0) {
            int year = new Date().getYear();
            for (int i = 0; i < list.size(); i++) {
                if ((list.get(i).getDateSend().getYear() - year) == 0) {
                     decimal = decimal.add(list.get(i).getClaimMoney());
                }
            }
            return decimal;
        }
        return decimal;
    }
}
